﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace RexToy.UI.WinForm.ViewModel
{
    public static class PropertyExtract
    {
        public static string ExtractName<T>(Expression<Func<T, object>> expr)
        {
            MemberExpression me = null;
            switch (expr.Body.NodeType)
            {
                case ExpressionType.MemberAccess:
                    me = expr.Body as MemberExpression;
                    break;

                case ExpressionType.Convert:
                    UnaryExpression ue = expr.Body as UnaryExpression;
                    me = ue.Operand as MemberExpression;
                    break;

                default:
                    Assertion.Fail("Unsupported expression.");
                    break;
            }
            return me.Member.Name;
        }
    }
}
